package com.pororo.smartoff;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class SmartOffRegisterService extends Service {
	final static String TAG = "SmartOffRegisterService";
	final static int NOTIFICATION_ID = 1000;
	SmartOffBroadcastReceiver receiver; 
	NotificationManager notiManager;

	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, "onCreate");
		
		notiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		receiver = new SmartOffBroadcastReceiver();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, "onStartCommand");

		Intent mainIntent = new Intent(this, SmartOffActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, mainIntent, 0);
		Notification mNotification = new Notification(R.drawable.lock_logo_for_noti, "Smart Off Running", System.currentTimeMillis());
		mNotification.setLatestEventInfo(this, "Smart Off", "Running", pendingIntent);
		mNotification.flags = mNotification.flags | Notification.FLAG_ONGOING_EVENT;
		
		startForeground(NOTIFICATION_ID, mNotification);		
		
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		filter.addAction(Intent.ACTION_USER_PRESENT);

//		filter.addAction(Intent.ACTION_BATTERY_CHANGED);
	
		registerReceiver(receiver, filter);
		
		return START_STICKY;
	}
	

	@Override
	public void onDestroy() {
		Log.i(TAG, "onDestory");
		unregisterReceiver(receiver);
		
		stopForeground(true);
		
		super.onDestroy();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

}
